package cn.ld.fj.service.food;

import cn.ld.fj.dao.food.FoodTypeDao;
import cn.ld.fj.dao.food.FoodTypePojoDao;
import cn.ld.fj.entity.FoodType;
import cn.ld.fj.pojo.FoodTypePojo;
import net.esoar.modules.orm.Page;
import net.esoar.modules.orm.PropertyFilter;
import net.esoar.modules.security.springsecurity.SpringSecurityUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 安全相关实体的管理类, 包括用户,角色,资源与授权类.
 *
 * @author fly
 */
//Spring Bean的标识.
@Component
//默认将类中的所有函数纳入事务管理.
@Transactional
public class FoodTypeManager {

    private static Logger logger = LoggerFactory.getLogger(FoodTypeManager.class);
    @Autowired
    private FoodTypeDao foodTypeDao;
    @Autowired
    private FoodTypePojoDao FoodTypePojoDao;


    //-- User Manager --//
    @Transactional(readOnly = true)
    public FoodType getOne(Long id) {
        return foodTypeDao.get(id);
    }

    public void save(FoodType entity) {
        foodTypeDao.save(entity);
    }


    public Page<FoodType> getPage(Page<FoodType> page, List<PropertyFilter> filters) {
        return foodTypeDao.findPage(page, filters);
    }

    public void delete(Long id) {
        foodTypeDao.delete(id);
    }

    public List<FoodType> getFirstLevel() {
        return foodTypeDao.findBy("pid", 0L);
    }

    public List<FoodType> getSecondFoodTypes(Long id) {
        return foodTypeDao.findBy("pid", id);
    }

    public Object getFoodTypeList() {
        String hql = "from cn.ld.fj.entity.FoodType ft";
        List<FoodType> foodTypeList = FoodTypePojoDao.find(hql);
        List<FoodTypePojo> foodTypePojo = getFoodTypePojo(foodTypeList);
        List<FoodTypePojo> foodTypePojoList = getChildTreeObjects(foodTypePojo, 0l);
        return foodTypePojoList;
    }

    public List<FoodTypePojo> getChildTreeObjects(List<FoodTypePojo> list, Long parentId) {
        List<FoodTypePojo> returnList = new ArrayList<FoodTypePojo>();
        for (Iterator<FoodTypePojo> iterator = list.iterator(); iterator.hasNext(); ) {
            FoodTypePojo t = (FoodTypePojo) iterator.next();
            // 一、根据传入的某个父节点ID,遍历该父节点的所有子节点
            if (t.getPid() != null && parentId != null
                    && t.getPid().equals(parentId)) {
                recursionFn(list, t);
                returnList.add(t);
            }
        }
        return returnList;
    }

    private void recursionFn(List<FoodTypePojo> list, FoodTypePojo t) {
        List<FoodTypePojo> childList = getChildList(list, t);// 得到子节点列表
        t.setFoodTypePojoList(childList);
        t.setNoDropdown(true);
        for (FoodTypePojo tChild : childList) {
            t.setNoDropdown(false);
            if (hasChild(list, tChild)) {// 判断是否有子节点
                // returnList.add(TreeObject);
                Iterator<FoodTypePojo> it = childList.iterator();
                while (it.hasNext()) {
                    FoodTypePojo n = (FoodTypePojo) it.next();
                    recursionFn(list, n);
                }
            }
        }
    }

    // 得到子节点列表
    private List<FoodTypePojo> getChildList(List<FoodTypePojo> list, FoodTypePojo t) {
        List<FoodTypePojo> tlist = new ArrayList<FoodTypePojo>();
        Iterator<FoodTypePojo> it = list.iterator();
        while (it.hasNext()) {
            FoodTypePojo n = (FoodTypePojo) it.next();
            if (n.getPid() != null && t.getId() != null
                    && n.getPid().equals(t.getId())) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    // 判断是否有子节点
    private boolean hasChild(List<FoodTypePojo> list, FoodTypePojo t) {
        return getChildList(list, t).size() > 0 ? true : false;
    }

    private List<FoodTypePojo> getFoodTypePojo(List<FoodType> foodTypeList) {
        List<FoodTypePojo> foodTypePojoList = new ArrayList<>();
        for (FoodType foodType : foodTypeList) {
            FoodTypePojo foodTypePojo = new FoodTypePojo();
            foodTypePojo.setId(foodType.getId());
            foodTypePojo.setPid(foodType.getPid());
            foodTypePojo.setName(foodType.getName());
            foodTypePojo.setNoDropdown(true);//没有
            foodTypePojoList.add(foodTypePojo);
        }
        return foodTypePojoList;
    }

    public List<FoodType> getAll() {
        return foodTypeDao.getAll();
    }
}
